package com.qin.EducationalManger.course.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mysql.cj.x.protobuf.MysqlxCrud.Delete;
import com.qin.EducationalManger.course.model.CourseModel;
import com.qin.EducationalManger.course.model.StudentCourseModel;
import com.qin.EducationalManger.course.model.namesModel;
import com.qin.EducationalManger.course.service.CourseService;
import com.qin.EducationalManger.course.service.Imp.CourseServiceImp;
import com.qin.EducationalManger.student.model.StudentModel;
import com.sun.javafx.sg.prism.NGShape.Mode;
import com.sun.org.apache.bcel.internal.generic.Select;

/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/CourseServlet")
public class CourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CourseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		String action = request.getParameter("action");
		
		Object res="";
		switch (action) {
		case "查询":
			 HashMap<String, Object> req= select(request);
			
			response.getWriter().write(new JSONObject(req).toString());
		
			break;
		case "添加":
			res = add(request).toString();
			response.getWriter().write((String)res);
			break;
		case "修改":
		    String upd =update(request)+" ";
		    response.getWriter().write(upd);
			
		   break;
		case "删除":
			String ree = delete(request)+"";
			response.getWriter().write(ree);
			
			break;
		case"查询课程":   
			Collection<namesModel> rec = selectCourse(request);
			
			
			response.getWriter().write(new JSONArray(rec).toString());
			break;
		case"查 询": //区分上个    这个中间 
			Map<String, Object> recc = page(request);
			
			response.getWriter().write(new JSONObject(recc).toString());
			
			break;

		default:
			break;
		}

		
		
		

	}
	//修改课程信息
	private Integer update(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String courseId = request.getParameter("courseId");
		String courseName = request.getParameter("courseName");
		String time = request.getParameter("time");
		String gradePoint_ = request.getParameter("gradePoint");
		
		Double gradePoint =null;
		if(gradePoint_!=null&&gradePoint_.intern().isEmpty()==false) {
			
			gradePoint = Double.parseDouble(gradePoint_);
			
		}	
		
		String teacherId = request.getParameter("teacherId");
		
		CourseModel model = new CourseModel();
		model.setCourseId(courseId);
		model.setCourseName(courseName);
		model.setGradePoint(gradePoint);
		model.setTime(time);
		model.setTeacherId(teacherId);
		
	

		
		
		
		return service.update(model);
	}

	//添加
	private Object add(HttpServletRequest request) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
    	CourseModel model = new CourseModel();
    	String courseId =request.getParameter("courseId");
        model.setCourseId(courseId);
    
    	List<CourseModel> models = service.select(model);//先查询数据库中是否已经存在该学号
    	

    	if(models.isEmpty()) {  //如果不存在，再往数据库插入
        		
    	String courseName =request.getParameter("courseName");
    	String gradePoint_ = request.getParameter("gradePoint");
    	Double gradePoint = null ;
    	if(gradePoint_!=null&&gradePoint_.intern().isEmpty()==false) {   //避免年龄为空  导致 空指针异常
    		gradePoint=Double.parseDouble(gradePoint_);
    	}
    	
    	String time =request.getParameter("time");
    	String teacherId =request.getParameter("teacherId");
    	
      	
    	model.setCourseName(courseName);
    	model.setGradePoint(gradePoint);
    	model.setTeacherId(teacherId);
    	
    	model.setTime(time);
    	
    	
    	
    	return service.insert(model);
    	}else {
			return 0;
		}
    	
	}

	//查询所有存在的课程信息
	private Map<String, Object> page(HttpServletRequest request) {
		// TODO Auto-generated method stub
		CourseModel model = new CourseModel();
		String courseId = request.getParameter("courseId");
		String courseName = request.getParameter("courseName");
		
		String pageIndex = request.getParameter("pageIndex");
		String pageLimit = request.getParameter("pageLimit");
		
		if(pageIndex==null) {
			return null;
		}
		
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);

		model.setCourseId(courseId);
		model.setCourseName(courseName);
		
		
		List<CourseModel> models =  service.select(model);
		int count = service.selectCount(model);
		
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", models);
		map.put("count", count);
		
		return map;
		
		
		
		
	}

	// 查询存在的课程的名字
	private List<namesModel> selectCourse(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
		CourseModel model = new CourseModel();
		List<namesModel>  names = service.selectCourse(model);
		
		return names;
	}
	private static CourseServiceImp service = new CourseServiceImp();
 

	
	
	
    //学生信息 删除 课程
	private Integer delete(HttpServletRequest request) {
		// TODO Auto-generated method stub
	
		String courseId = request.getParameter("courseId");
	
		

		
		CourseModel model = new CourseModel();
		
		
		model.setCourseId(courseId);
	
		return service.delete(model);
		
		
		
	}

	
	private HashMap<String, Object> select(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
		
		String courseId = request.getParameter("courseId");
		
		
		
		if(courseId==null) {
			return null;
		}
		
		
	     String  courseIds[] =	courseId.split(" ");
		
		
		
		List<StudentCourseModel> models = service.select(courseIds);
		   
		HashMap<String, Object> list = new HashMap<String, Object>();
		
		
		list.put("lists", models);
		return  list;
		 
		
	
	}

}
